﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using Ultima;
using Expand;
using System.Windows.Forms;
using System.ComponentModel;
using System.Reflection;

namespace SpellCaster3.Core.Drawing
{
    public class UltimaIconBitmap
    {
        private RectangleF _AliasRectangle = new RectangleF(2F, 2F, 40F, 40F);
        protected virtual RectangleF AliasRectangle { get { return _AliasRectangle; } }

        private RectangleF _KeyNameRectangle = new RectangleF(2F, 2F, 40F, 15F);
        protected virtual RectangleF KeyNameRectangle { get { return _KeyNameRectangle; } }

        private int _HueIndex = 1;
        private int _GumpIndex = 0;
        private string _Alias = string.Empty;
        private string _KeyName = string.Empty;
        private bool _Enabled = true;

        protected bool Invalidated { get; private set; }

        public bool AutoRender { get; set; }

        public int HueIndex
        {
            get { return _HueIndex; }
            set
            {
                Enabled = Enabled ? !(value < 1 || value > Hues.List.Length) : Enabled;
                if (value != _HueIndex)
                {
                    _HueIndex = value;
                    Invalidate();
                }
            }
        }

        public int GumpIndex
        {
            get { return _GumpIndex; }
            set
            {
                if (value != _GumpIndex)
                {
                    _GumpIndex = value;
                    Invalidate();
                }
            }
        }

        public string Alias
        {
            get { return _Alias; }
            set
            {
                if (_Alias != value)
                {
                    _Alias = value;
                    Invalidate();
                }
            }
        }

        public string KeyName
        {
            get { return _KeyName; }
            set
            {
                if (_KeyName != value)
                {
                    _KeyName = value;
                    Invalidate();
                }
            }
        }

        public bool Enabled
        {
            get { return _Enabled; }
            set
            {
                if (_Enabled != value)
                {
                    _Enabled = value;
                    OnEnabledChanged(this, EventArgs.Empty);
                    Invalidate();
                }
            }
        }

        public UltimaIconBitmap()
        {
            try
            {
                _GumpIndex = Forms.GumpPicker.BaseRange.Minimum;
            }
            catch { }
            AutoRender = true;
        }

        public static implicit operator Bitmap(UltimaIconBitmap icon)
        {
            return icon.Image;
        }

        public Bitmap Image { get; private set; }

        public event EventHandler EnabledChanged;

        protected virtual void Render()
        {
            Bitmap bmp;
            Graphics g;

            if (!Invalidated) return;

            if (!Enabled || GumpIndex < 0)
            {
                bmp = new Bitmap(SpellCaster3.Properties.Resources.BaseIcon.Width, SpellCaster3.Properties.Resources.BaseIcon.Height);
                g = Graphics.FromImage(bmp);
                g.FillRectangle(SystemBrushes.FromSystemColor(Control.DefaultBackColor), 0, 0, bmp.Width, bmp.Height);
            }
            else
            {
                if (string.IsNullOrWhiteSpace(Alias))
                {
                    Bitmap tmp;
                    if (HueIndex > 1)
                        tmp = Gumps.GetGump(GumpIndex, Hues.GetHue(HueIndex - 1), false);
                    else
                        tmp = Gumps.GetGump(GumpIndex);
                    bmp = new Bitmap(tmp.Width, tmp.Height);
                    g = Graphics.FromImage(bmp);
                    g.DrawImage(tmp, 0f, 0f);
                    tmp.Dispose();
                }
                else
                {
                    //Icona base + scritta
                    bmp = new Bitmap(SpellCaster3.Properties.Resources.BaseIcon.Width, SpellCaster3.Properties.Resources.BaseIcon.Height);
                    g = Graphics.FromImage(bmp);
                    g.DrawImage(SpellCaster3.Properties.Resources.BaseIcon, 0F, 0F);
                    if (HueIndex > 1) Hues.GetHue(HueIndex - 1).ApplyTo(bmp, false);
                    g.DrawString(Alias, Control.DefaultFont, Brushes.Black, AliasRectangle);
                }
                PrintKeyName(g);
            }
            g.Flush();
            g.Dispose();
            if (Image != null) Image.Dispose();
            Image = bmp;
            Invalidated = false;
        }

        private void PrintKeyName(Graphics g)
        {
            if (!string.IsNullOrWhiteSpace(KeyName))
            {
                Font font = new Font(Control.DefaultFont, FontStyle.Bold);
                g.DrawString(KeyName, font, Brushes.Black, KeyNameRectangle);
            }
        }

        protected void OnEnabledChanged(object sender, EventArgs e)
        {
            if (EnabledChanged != null) EnabledChanged(sender, e);
        }

        public void Refresh()
        {
            Invalidated = true;
            Render();
        }

        protected void Invalidate()
        {
            Invalidated = true;
            if (AutoRender) Render();
        }
    }
}
